package com.qingcheng.consumer;

import com.qingcheng.pojo.order.Order;
import com.qingcheng.service.OrderService;
import com.qingcheng.service.WeixinPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
@RabbitListener(queues = "order.cancel")
public class CancelOrderReceiver {

    @Autowired
    private WeixinPayService weixinPayService;

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void handle(Long orderId){
        log.info("接收延迟消息 orderId:{}", orderId);
        Order order1 = orderService.queryById(orderId + "");
        if("0".equals(order1.getOrderStatus())){
            //查询订单
            Map<String, String> map = weixinPayService.queryPayOrderByWeChat(orderId+"");
            if("SUCCESS".equals(map.get("return_code"))&&"NOTPAY".equals(map.get("trade_state"))){
                //关闭订单
                log.info("关闭订单", orderId);
                orderService.closeOrder(orderId+"",map.get("transaction_id"));
            }
        }
    }
}
